package com.yaopaikeji.controller;

import com.yaopaikeji.constants.RestResult;
import com.yaopaikeji.model.EmailGroupInfo;
import com.yaopaikeji.model.UserGroupRelation;
import com.yaopaikeji.service.EmailGroupService;
import com.yaopaikeji.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/email-group")
public class EmailGroupController {

    @Autowired
    private EmailGroupService emailGroupService;

    @PostMapping("/add")
    public RestResult<EmailGroupInfo> addNewEmailGroup(@RequestBody EmailGroupInfo emailGroupInfo){
        emailGroupInfo.setCreateTime(TimeUtil.now());

        return new RestResult<>(emailGroupService.addNewGroup(emailGroupInfo));
    }

    @PostMapping("/update")
    public RestResult<EmailGroupInfo> updateEmailGroup(@RequestBody EmailGroupInfo emailGroupInfo){

        return new RestResult<>(emailGroupService.updateGroupInfo(emailGroupInfo));
    }

    @GetMapping("/delete/{id}")
    public RestResult deleteEmailGroup(@PathVariable("id") Integer id){

        return new RestResult(emailGroupService.deleteEmailGroup(id));
    }

    @GetMapping("/{id}")
    public RestResult<EmailGroupInfo> getEmailGroup(@PathVariable("id") Integer id){

        return new RestResult<>(emailGroupService.findByGroupId(id));
    }

    @GetMapping("/all")
    public RestResult<List<EmailGroupInfo>> getEmailGroupAll(){

        return new RestResult<>(emailGroupService.listAllGroups());
    }

    @PostMapping("/add-member")
    public RestResult addMemberToGroup(@RequestBody UserGroupRelation userGroupRelation){

        return new RestResult(emailGroupService.addMemberToEmailGroup(userGroupRelation));
    }

    @PostMapping("/add-members")
    public RestResult addMembersToGroup(@RequestBody List<UserGroupRelation> userGroupRelations){

        return new RestResult(emailGroupService.addMembersToEmailGroup(userGroupRelations));
    }

    @PostMapping("/remove-member")
    public RestResult rmoveMemberFromGroup(@RequestBody UserGroupRelation userGroupRelation){

        return new RestResult(emailGroupService.deleteUserFromGroup(userGroupRelation));
    }

}
